﻿var List = {};

(function () {
    List.PlayerList = function () {
        var _playerList = new Array();

        function iterateOverPosition(position) {
            var returnArray = new Array();
            for (var i = 0; i < _playerList.length; i++) {
                if (_playerList[i]["position"] === position) {
                    returnArray.push(_playerList[i]);
                }
            }
            return returnArray;
        };


        return {
            addElement: function (element) {
                _playerList.push(element);
            },
            removeElement: function (id) {
                for (var i = 0; i < _playerList.length; i++) {
                    if (_playerList[i]["playerID"] = id) {
                        
                    }
                }
                
            },
            populateList: function (ajaxSettings) {
                $.ajaxSetup(ajaxSettings);
                $.ajax(
                    {
                        dataType: "json",
                        success: function (response) {
                            var playerList = JSON.parse(response.d);
                            for (var i = 0; i < playerList.length; i++) {
                                _playerList.push(playerList[i]);
                            }
                        }
                    });
            },
            getKeepers: function () {
                return iterateOverPosition(0);
            },
            getDefenders: function () {
                return iterateOverPosition(1);
            },
            getMidfielders: function () {
                return iterateOverPosition(2);
            },
            getForwards: function () {
                return iterateOverPosition(3);
            },
            getPlayerFromTeam: function (id) {
                var returnArray = new Array();
                for (var i = 0; i < _playerList.length; i++) {
                    if (_playerList[i]["realTeamID"] === id) {
                        returnArray.push(_playerList[i]);
                    }
                }
                return returnArray;
            },
            getAllPlayers: function () {
                return _playerList;
            },
            getPlayersFrom: function (team, position) {
                var posArray = iterateOverPosition(position);
                var returnArray = new Array();
                for (var i = 0; i < posArray.length; i++) {
                    if (posArray[i]["realTeamID"] === id) {
                        returnArray.push(_playerList[i]);
                    }
                }
                return returnArray;

            }
        };
    };
})();
